﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Net.NetworkInformation;
using Zhao.Common.Helper;
using Zhao.IServer;
using Zhao.IServer.User;
using Zhao.Models.DTO;
using Zhao.Models.Entity;

namespace Zhao.Arena.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class OrderController : ControllerBase
    {
        private readonly IOrdersServer _ordersServer;
        private readonly IUsersServer _usersServer;
        public OrderController(IOrdersServer ordersServer, IUsersServer usersServer)
        {
            _ordersServer = ordersServer;
            _usersServer = usersServer;
        }


        /// <summary>
        /// 返回订单信息
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="searchaString"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("{pageIndex:int}/{pageSize:int}")]
        [Route("{pageIndex:int}/{pageSize:int}/{searchaString}")]
        public async Task<JsonResult> GetInfo(int pageIndex = 1, int pageSize = 10, string searchaString = " ")
        {
            var result = new ApiResult<PageModel<OrderDTO>>
            {
                Message = "操作失败",
                Success = false
            };

            Expressionable<Orders, Users> expressionable = new Expressionable<Orders, Users>();

            //根据用户姓名查询？？？还是根据什么查询订单表条件
            expressionable.AndIF(!searchaString.IsNullOrWhiteSpace(), (a, b) => b.Username.Contains(searchaString));

            var data = await _ordersServer.QueryOrderPage(expressionable.ToExpression(), pageIndex, pageSize);

            if (data != null)
            {
                result.Success = true;
                result.Message = "操作成功";
                result.Data = data;
            }
            return new JsonResult(result);
        }
    }
}
